package com.wangy.job;

import com.wangy.bean.MonitorDetail;
import com.wangy.service.SchedulerService;
import com.wangy.util.SpringUtils;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * 查询表格字段的空值率，并插入到 mysql_data_monitor 中
 * @author yyzb
 * @date 2024/12/30 18:38
 */

public class MySQLMonitorJob implements Job {

    SchedulerService schedulerService = SpringUtils.getBean(SchedulerService.class);

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        JobDataMap dataMap = jobExecutionContext.getJobDetail().getJobDataMap();
        String databaseName = dataMap.getString("databaseName");
        String tableName = dataMap.getString("tableName");
        String fieldName = dataMap.getString("fieldName");

        MonitorDetail monitorDetail = new MonitorDetail();
        monitorDetail.setDatabaseName(databaseName);
        monitorDetail.setTableName(tableName);
        monitorDetail.setFieldName(fieldName);
        Double nullRate = schedulerService.computeNullRate(tableName, fieldName);
        monitorDetail.setFieldNullRate(nullRate);
        schedulerService.insertRecord(monitorDetail);
    }
}
